﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;

namespace iBackupExplorer.Data
{
    internal class AdressBookDal : IDisposable
    {
        private static readonly AdressBookDal _adressBookDalInstance = new AdressBookDal();
        private readonly AdressBookContext _objEntities;

        static AdressBookDal()
        {
        }

        private AdressBookDal()
        {
            _objEntities = new AdressBookContext();
        }

        public static AdressBookDal GetInstance
        {
            get { return _adressBookDalInstance; }
        }

        #region IDisposable Members
        public void Dispose()
        {
            _objEntities.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion

        public AdressBook getPersonn(string adress)
        {
            if (string.IsNullOrWhiteSpace(adress) == false)
            {
                adress = adress.Replace(" ", "");
                if (adress.Length > 9)
                    adress = adress.Substring(adress.Length - 9);
            }

            long? id = (from items in _objEntities.ABMultiValue
                        where items.value.Replace(" ", "").Contains(adress)
                        select items.ROWID).FirstOrDefault();


            AdressBook personn = (from contact in _objEntities.AdressBook
                                   where contact.ROWID == id
                                   select contact).FirstOrDefault();


            return personn;

        }
        public IList getPersonn()
        {

            var personn = from contact in _objEntities.AdressBook.Include(x=>x.ABMultiValue5)
                          orderby contact.Last, contact.First, contact.Organization
                          select contact;

            var postal = from items in _objEntities.ABMultiValueEntry
                         select items;

            IList labels = getLabel();

            foreach (AdressBook adress in personn)
            {
                if (adress.ABMultiValue5 != null)
                {
                    foreach (ABMultiValue value in adress.ABMultiValue5)
                    {
                        if (value.label != null)
                            value.LongLabel = labels[((int) value.label) - 1].ToString();
                        else
                            value.LongLabel = "Other : ";

                        ABMultiValue value1 = value;
                        var list = from addPostal in postal
                                   where addPostal.parent_id == value1.UID
                                   select addPostal;

                        if (list.Any())
                            foreach (ABMultiValueEntry postalAdress in list.OrderBy(x => x.key))
                                value.PostalAdress += postalAdress.value + " ";

                        if (string.IsNullOrWhiteSpace(value.PostalAdress) == false)
                            value.PostalAdress = value.PostalAdress.Trim();
                    }
                }

            }
            return personn.ToList();

        }

        public IList getLabel()
        {
            var label = from items in _objEntities.ABMultiValueLabel
                        select items;

            List<String> labels = new List<string>();
            foreach (ABMultiValueLabel abMultiValueLabel in label)
            {
                if (abMultiValueLabel.value.StartsWith("_$!<"))
                    labels.Add(abMultiValueLabel.value.Replace("_$!<", "").Replace(">!$_", " : "));
                else
                    labels.Add(abMultiValueLabel.value);
            }

            return labels;

        }
    }
}
